// gulp主类
var gulp = require('gulp');
var del = require('del');
var gutil = require('gulp-util');
var webpack = require('webpack');
var uglify = require('gulp-uglify');
var cssMin = require('gulp-minify-css');
var path = require('path');
var node_modules = path.resolve(__dirname, 'node_modules');
var pathToReact = path.resolve(node_modules, 'react/react');
var pathToReactDom = path.resolve(node_modules, 'react-dom/index');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
config = {
    entry: [path.resolve(__dirname, 'js/main.js')],
    output: {
        path: path.resolve(__dirname, 'build1'),
        filename: 'bundle.js'
    },
    resolve: {
        alias: {
            'react': pathToReact,
            'react-dom': pathToReactDom
        },
        extensions: ['', '.js', '.less']
    },
    module: {
        loaders: [{
            test: /\.jsx?$/,
            loader: 'jsx-loader'
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
        }, {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
        }],
        noParse: [pathToReact, pathToReactDom]
    },
    plugins: [commonsPlugin, new ExtractTextPlugin('[name].css')]
};
gulp.task('del', function () {
    return del(['build/**.*', '!build/index.html']).then(paths => {
        console.log('Deleted files and folders:\n', paths.join('\n'));
    });
});
gulp.task('webpack', ['del'], function (callback) {
    // run webpack
    webpack(config, function (err, stats) {
        if (err) throw new gutil.PluginError('webpack', err);
        gutil.log('[webpack]', stats.toString({
            // output options
        }));
        callback();
    });
});
gulp.task('uglify', ['webpack'], function () {
    return gulp.src('build1/**.js').pipe(uglify({
        // 混淆变量名
        mangle: true,
        // 输出时将所有的中文转换为unicode
        output: {ascii_only: true}
    })).pipe(gulp.dest('build'));
});
gulp.task('cssMin', ['uglify'], function () {
    return gulp.src('build1/**.css').pipe(cssMin()).pipe(gulp.dest('build'));
});
gulp.task('default', ['cssMin']);